package br.ufrj.ppgi.jemf_advanced_example.plan;

import java.util.Date;

import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ScrollView;
import br.ufrj.ppgi.jemf.mobile.bean.Plan;
import br.ufrj.ppgi.jemf.mobile.view.PlanView;
import br.ufrj.ppgi.jemf.utils.DateTimeFormat;
import br.ufrj.ppgi.jemf_advanced_example.BuildConfig;

/**
 * A fragment representing a single Plan detail screen.
 * This fragment is either contained in a {@link PlanListActivity} in two-pane mode (on tablets) or a {@link PlanDetailActivity} on handsets.
 */
public class PlanDetailFragment extends Fragment {
    /**
     * The serialization (saved instance state) Bundle key representing the activated item position.
     * Only used on tablets.
     */
    private static final String STATE_PLAN = "activated_plan";
	/**
     * The fragment argument representing the item ID that this fragment represents.
     */
    public static final String ARG_ID = "PLAN";
    /**
     * The Plan content this fragment is showing.
     */
    private static Plan plan;
    /**
     * The Plan view holder.
     */
    private static PlanView planView;



    /**
     * Mandatory empty constructor for the fragment manager to instantiate the fragment (e.g. upon screen orientation changes).
     */
    public PlanDetailFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the Plan content specified by the fragment arguments.
        if (getArguments().containsKey(PlanDetailFragment.ARG_ID)) {
        	plan = getArguments().getParcelable(PlanDetailFragment.ARG_ID);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	    // Show the Plan content
	    if (plan != null) {
	    	planView = new PlanView(getActivity(), false);
	    	// Description
	    	planView.setPlanDescriptionText(plan.getDescription());
	    	// Priority
	    	planView.setPlanPriorityText(plan.getPriorityString());
	    	// Creation Date
	    	planView.setPlanCreationDateText(plan.getCreationDateString());
	    	// Last Modification Date
	    	planView.setPlanLastModificationDateText(plan.getLastModificationDateString());
	    	// Objective
	    	planView.setPlanObjectiveText(plan.getObjective());
	    	// Observation
	    	planView.setPlanObservationText(plan.getObservation());
	    	// Check List
	    	planView.setPlanCheckListText(plan.getCheckList());
	    	// Risk
	    	planView.setPlanRiskText(plan.getRisk());

	    	// Its necessary to add the PlanView inside a ScrollView to show scrolls
	    	ScrollView scrollView = new ScrollView(getActivity());
	    	scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	    	// Add Plan View as a child of the ScrollView.
	    	scrollView.addView(planView);
	    	// Add a Tag to the view location
	    	scrollView.setTag(ARG_ID);

			return scrollView;
    	} else {
        	return null;
    	}
    }

    @Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
        // Restore the previously serialized activated Plan
        if (savedInstanceState != null && savedInstanceState.containsKey(STATE_PLAN)) {
        	plan = savedInstanceState.getParcelable(STATE_PLAN);
        }
	}





	/**
     * Get updated Plan
     * @return Plan
     */
	public static Plan getPlan() {
		if (BuildConfig.DEBUG) Log.i(PlanListActivity.TAG, "PlanDetailFragment ::: getPlan");
    	return plan;
    }

	/**
	 * Update Plan data from UI components
	 */
	public static void updateDataFromUI() {
    	if ((plan != null) && (planView != null)) {
    		if (BuildConfig.DEBUG) Log.i(PlanListActivity.TAG, "PlanDetailFragment ::: updateDataFromUI");
    		// Update Plan object with new data included by user through components
    		plan.setDescription(planView.getPlanDescriptionText());
    		plan.setPriorityString(planView.getPlanPriorityText());
    		plan.setCreationDateString(planView.getPlanCreationDateText());
			Date now = new Date(System.currentTimeMillis());
			planView.setPlanLastModificationDateText(DateTimeFormat.DateTimeToString(now));
    		plan.setLastModificationDateString(planView.getPlanLastModificationDateText());
    		plan.setObjective(planView.getPlanObjectiveText());
    		plan.setObservation(planView.getPlanObservationText());
    		plan.setCheckList(planView.getPlanCheckListText());
    		plan.setRisk(planView.getPlanRiskText());
        }
    }



    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Force update data from UI
        updateDataFromUI();
        // Serialize and persist the activated Plan.
        outState.putParcelable(STATE_PLAN, plan);
    }
}
